//
//  HomePageCollectionViewController.m
//  Food choices
//
//  Created by xalo on 16/2/29.
//  Copyright © 2016年 张广斌. All rights reserved.
//

#import "HomePageCollectionViewController.h"
#import "HomePagelCollectionViewCell.h"
//#import "CategoryHelper.h"
//#import "HomePageTableViewController.h"
//#import "SubCatTableViewController.h"

@interface HomePageCollectionViewController ()
//分类信息
@property (nonatomic,strong) NSMutableArray *SubCategoryArray;

//@property (nonatomic,strong) SubCategory *selectedSubCategory;


@end

@implementation HomePageCollectionViewController

static NSString * const reuseIdentifier = @"Cell";


//请求分类数据
//- (void)fecthCategoryData
//{
//    //在数据库中获取数据
//    self.SubCategoryArray = [[[CategoryHelper sharedManager] selectAll] mutableCopy];
//    
//    //判断数组是否为空
//    if (self.SubCategoryArray.count == 0) {
//        [[HTTPHepler sharedManager] fetchDataWithUrl:kCategories parameter:nil species:5 block:^{
//            NSArray *TGarray = [[HTTPHepler sharedManager] arryWithSpecies:5];
//            
//            TGCategory *cate = [[TGCategory alloc] init];
//            
//            for (TGCategory *category in TGarray) {
//                if ([category.cat_name isEqualToString:@"美食"]) {
//                    
//                    cate = category;
//                }
//                NSLog(@"cateName = %@",category.cat_name);
//                
//            }
//            
//            [self.SubCategoryArray addObjectsFromArray:cate.subcats];
//            
//            for (NSInteger i = 0; i < self.SubCategoryArray.count; i++) {
//                SubCategory *subCate = self.SubCategoryArray[i];
//                
//                [[CategoryHelper sharedManager] insertDataWithModel:subCate];
//                
//                NSLog(@"%@",subCate.subcat_name);
//                
//            }
//            
//        }];
//
//    }
//    
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    [self fecthCategoryData];
    
    
    // 创建布局对象
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    if (kScreenWidth > 375) {
        CGFloat itemWidth = (kScreenWidth - 2 * 5 - 5 * 10) / 6;
        CGFloat itemHeight = itemWidth;
        flowLayout.itemSize = CGSizeMake(itemWidth, itemHeight);
        flowLayout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);// 分区内边距
        flowLayout.minimumLineSpacing = 10;// 行间距
        flowLayout.minimumInteritemSpacing = 10;// 元素间距
        
    }
    else if (kScreenWidth == 375)
    {
        CGFloat itemWidth = (kScreenWidth - 5 - 10/2 - 4 * 10) / 5;
        CGFloat itemHeight = itemWidth;
        flowLayout.itemSize = CGSizeMake(itemWidth, itemHeight);
        flowLayout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);// 分区内边距
        flowLayout.minimumLineSpacing = 10;// 行间距
        flowLayout.minimumInteritemSpacing = 10;// 元素间距
        
    }
    else
    {
        CGFloat itemWidth = (kScreenWidth - 2 * 5 - 4 * 10) / 5;
        CGFloat itemHeight = itemWidth;
        flowLayout.itemSize = CGSizeMake(itemWidth, itemHeight);
        flowLayout.sectionInset = UIEdgeInsetsMake(10, 5, 10, 5);// 分区内边距
        flowLayout.minimumLineSpacing = 10;// 行间距
        flowLayout.minimumInteritemSpacing = 10;// 元素间距
        
    }
    
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;// 水平滚动
    
    // 集合视图
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) collectionViewLayout:flowLayout];
    
    // 设置collectionview背景图
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.collectionView.frame];
    
    imageView.image = [UIImage imageNamed:@"collectionView.jpg"];
    
    self.collectionView.backgroundView = imageView;

    
    self.collectionView.dataSource = self;// 设置数据源
    
    self.collectionView.delegate = self;// 设置代理
    
    [self.collectionView registerClass:[HomePagelCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];// 注册单元格
    
    self.collectionView.backgroundColor = [UIColor whiteColor];
    
    self.collectionView.pagingEnabled = YES;
    
    // Register cell classes
//    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark <UICollectionViewDataSource>
// 集合视图中的分区数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

// 分区中元素个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 49;
}

// 加载单元格
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HomePagelCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    cell.backgroundColor = [UIColor yellowColor];
    
//    NSArray *array = KArray;
//    
    cell.label.text = @"12345";
    
    cell.layer.cornerRadius = CGRectGetHeight(cell.frame) / 2;

    cell.backgroundColor = [UIColor colorWithRed:(arc4random()%100 +150)/255.0 green:(arc4random()%100+110)/255.0 blue:(arc4random()%100+130)/255.0 alpha:0.8];

    return cell;
    
}


//- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
//{
//    NSString *subcat_name = KArray1[indexPath.item];
//    
//    if (self.SubCategoryArray != 0) {
//        for (SubCategory *subCate in self.SubCategoryArray) {
//            
//            if ([subCate.subcat_name isEqualToString:subcat_name]) {
//                self.selectedSubCategory = subCate;
//                
//                NSLog(@"%@",self.selectedSubCategory.subcat_name);
//            }
//            
//        }
//    }
//
//    HomePageTableViewController *homePageVC = (HomePageTableViewController *)self.collectionView.nextResponder.nextResponder.nextResponder;
//    
//    SubCatTableViewController *subCatVC = [[SubCatTableViewController alloc] initWithStyle:(UITableViewStylePlain)];
//    
//    subCatVC.selectCity = homePageVC.selectCity;
//    subCatVC.selectDis = homePageVC.selectDis;
//    subCatVC.selectBiz = homePageVC.selectBiz;
//    subCatVC.currentCoordinate2D = homePageVC.currentCoordinate2D;
//    if (self.selectedSubCategory) {
//       subCatVC.subCategory = self.selectedSubCategory;
//    }
//    
//    subCatVC.currentCoordinate2D = homePageVC.currentCoordinate2D;
//    
//    [homePageVC.navigationController pushViewController:subCatVC animated:YES];
//    
//}


#pragma mark <UICollectionViewDelegate>

/*
// Uncomment this method to specify if the specified item should be highlighted during tracking
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
	return YES;
}
*/

/*
// Uncomment this method to specify if the specified item should be selected
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
*/

/*
// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
	return NO;
}

- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	return NO;
}

- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	
}
*/

@end
